package com.smartleanx.module.hrm.service.esign.service.impl;

import cn.hutool.json.JSONObject;
import com.smartleanx.framework.security.core.util.SecurityFrameworkUtils;
import com.smartleanx.module.hrm.dal.dataobject.employee.EmployeeDO;
import com.smartleanx.module.hrm.dal.mysql.employee.EmployeeMapper;
import com.smartleanx.module.hrm.dal.mysql.esign.EsignPsnSealMapper;
import com.smartleanx.module.hrm.service.esign.auth.impl.EsignSealUrlService;
import com.smartleanx.module.hrm.service.esign.service.ElecPsnSealService;
import com.smartleanx.module.hrm.service.esign.vo.SealCreateUrlReq;
import com.smartleanx.module.hrm.service.esign.vo.SealUrlCommonResp;
import com.smartleanx.module.hrm.service.examine.util.exception.ResultException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class ElecPsnSealServiceImpl implements ElecPsnSealService {

    @Autowired
    private EsignPsnSealMapper esignPsnSealMapper;

    @Autowired
    private EsignSealUrlService esignSealUrlService;
    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public String createPanSealUrl( String redirectUrl) {
        Long loginUserId = SecurityFrameworkUtils.getLoginUserId();
        EmployeeDO employee = employeeMapper.selectByUserId(loginUserId);
        if(ObjectUtils.isEmpty(employee)||employee.getEsignAuthorizedStatus()==0){
            throw new ResultException("请先进行个人认证");
        }
        SealCreateUrlReq sealCreateUrlReq = new SealCreateUrlReq();
        List<String> uneditableFields=new ArrayList<>();
        uneditableFields.add("sealCreateMode");
        sealCreateUrlReq.setUneditableFields(uneditableFields);
        sealCreateUrlReq.setRedirectUrl(redirectUrl);
        sealCreateUrlReq.setPsnId(employee.getEsignPsnId());
        try {
            SealUrlCommonResp sealUrlCommonResp = esignSealUrlService.getPsnSealsManageUrl(employee.getEsignPsnId());
            String psnSealCreateUrl = sealUrlCommonResp.getPsnSealManageUrl();
            return psnSealCreateUrl;
        } catch (Exception e) {
            System.err.println(e.getMessage());
        }
        return "";
    }
}
